import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: const Text("Transform Widget Demo"),
      ),
      body: WillPopScopeWidget(),
    ),
  ));
}

class WillPopScopeWidget extends StatefulWidget {

  @override
  _WillPopScopeWidgetState createState() => _WillPopScopeWidgetState();
}

class _WillPopScopeWidgetState extends State<WillPopScopeWidget>{

  DateTime? _lastPressedAt;

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        if (_lastPressedAt == null ||
        DateTime.now().difference(_lastPressedAt!) > Duration(seconds: 1)) {
          // 两次点击时间间隔超过1秒重新计时
          _lastPressedAt = DateTime.now();
          return false;
        }
        return true;
      },
      child: Container(
        alignment: Alignment.center,
        child: const Text('1秒内连续点击两次返回键退出'),
      ),
    );
  }
}
